/**
 * FileName: NettyServerInitializer
 * Author: Mr.Wang
 * Date: 2025/10/15 16:08
 * Description:
 * History:
 */
package org.test.device.config.netty;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

/**
 * 〈一句话功能简述〉<br>
 * 〈独立的Netty服务器初始化器〉
 * 异步启动Netty服务器，避免阻塞主线程
 *
 * @author Mr.Wang
 * @create 2025/10/15
 * @since 1.0.0
 */
@Slf4j
@Component
public class NettyServerInitializer implements CommandLineRunner {

    @Resource
    private NettyServer nettyServer;

    @Resource
    @Qualifier("nettyInitTaskExecutor")
    private TaskExecutor nettyInitTaskExecutor;


    @Override
    public void run(String... args) throws Exception {
        // 用专用线程池执行 Netty 启动，不影响业务线程池
        nettyInitTaskExecutor.execute(() -> {
            log.info("[NettyServerInitializer]准备启动Netty服务器（专用线程池）...");
            try {
                nettyServer.start();
            } catch (Exception e) {
                log.error("[NettyServerInitializer]Netty服务器启动失败", e);
            }
        });
        // 主线程不阻塞，继续执行其他初始化逻辑
        log.info("[NettyServerInitializer]主线程继续执行其他初始化任务");
    }
}